package com.oa.android.activity;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.oa.android.R;
import com.oa.android.adapter.FtypeAdapter;
import com.oa.android.adapter.FujAdapter;
import com.oa.android.adapter.RicTypeAdapter;
import com.oa.android.entry.EmilJs;
import com.oa.android.entry.FileType;
import com.oa.android.entry.Fjian;
import com.oa.android.entry.Gwsf;
import com.oa.android.entry.RicType;
import com.oa.android.utils.ImageUtlis;
import com.oa.android.utils.UConstants;
import com.oa.android.utils.Util;
import com.oa.android.view.MyGridView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 添加传阅文件
 *
 * @author -zouqh
 */
public class AddCywjActivity extends Activity implements OnClickListener {

    private ImageView close;
    private TextView title, send,sendTo, addfile, fuj,gwType,gwFl;
    private EditText  emilTitle, emilContent;
    private Spinner spinner1,spinner2;
    private List<File> listFile;
    private Dialog Jdialog;
    ProgressDialog dialog;

    private List<FileType> fileTypeList;
    private Dialog typeDialog;
    private List<RicType> ricList;

    private String idStr;
    private String typeGw;
    private String fileId;
    private Gwsf gwsf;
    private String personID="";
    private MyGridView listFj;
    private FujAdapter adapter;
    List<Fjian> list;
    private int num;
    private List<EmilJs> jsList;
    private StringBuffer stringBufferN;
    private StringBuffer stringBufferId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        gwsf = (Gwsf) getIntent().getSerializableExtra("gwsf");
        setContentView(R.layout.add_cywj_activity);
        close = (ImageView) findViewById(R.id.close);
        title = (TextView) findViewById(R.id.title_text);
        send = (TextView) findViewById(R.id.ritgh_text);
        sendTo = (TextView) findViewById(R.id.sendTo);
        emilTitle = (EditText) findViewById(R.id.emilTitle);
        addfile = (TextView) findViewById(R.id.addFile);
        fuj = (TextView) findViewById(R.id.fuj);
        gwType = (TextView) findViewById(R.id.gwtype);
        gwFl = (TextView) findViewById(R.id.gwFl);
        emilContent = (EditText) findViewById(R.id.emilContent);
        spinner1=(Spinner)findViewById(R.id.spiner1);
        spinner2=(Spinner)findViewById(R.id.spiner2);
        listFj=(MyGridView)findViewById(R.id.listFj);
        listFile = new ArrayList<>();
        stringBufferN=new StringBuffer();
        stringBufferId=new StringBuffer();
        if (null!=gwsf){
            send.setText("保存");
            title.setText("修改传阅");
            idStr=gwsf.getID();
            emilTitle.setText(gwsf.getTITLE());
            emilContent.setText(gwsf.getCONTENT());
            gwType.setText(gwsf.getGWLX());
            sendTo.setText(gwsf.getJSEMPNAMES());
            gwFl.setText(gwsf.getDIRNAME()+"");
//            gwFl.setText(gwsf.get);
            fileId=gwsf.getDIRID();
            typeGw=gwsf.getGWLX();
            getFj();
            getjsPerson();
        }else{
            list=new ArrayList<>();
            adapter=new FujAdapter(AddCywjActivity.this,list);
            listFj.setAdapter(adapter);
            send.setText("提交");
            title.setText("新增传阅");
            getId();
        }


        getFtype("2");
        getRictype();

        send.setVisibility(View.VISIBLE);

        close.setOnClickListener(this);
        send.setOnClickListener(this);
        sendTo.setOnClickListener(this);
        addfile.setOnClickListener(this);
        gwFl.setOnClickListener(this);
        gwType.setOnClickListener(this);
        fuj.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (fuj.getText().length()>0){
                    pointDialog("确定清除附件吗？");
                }

                return false;
            }
        });
        listFj.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("listFj---",position+"---");
                if (Util.NoNull(list.get(position).getXH())){
                    deleteFujian(position);
                }else{
                    list.remove(position);
                    listFile.remove(position-num);
                    adapter.notifyDataSetChanged();
                }
                return false;
            }
        });



    }


    /**
     * 选择类型
     */
    private void showtype(String title) {
        LayoutInflater inflaterDl = LayoutInflater.from(this);
        View layout = inflaterDl.inflate(R.layout.sb_state_dailog,
                null);
        // 对话框
        typeDialog = new Dialog(this);
        typeDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        typeDialog.show();

        typeDialog.setContentView(layout);
        ListView listView = (ListView) layout.findViewById(R.id.statesListView);
        TextView textView=(TextView)layout.findViewById(R.id.name);
        textView.setText(title);
        listView.setAdapter(new FtypeAdapter(this, fileTypeList));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                typeDialog.dismiss();
                fileId=fileTypeList.get(position).getID();
                gwFl.setText(fileTypeList.get(position).getDIRNAME());

            }
        });

    }

    /**
     * 选择归档
     */
    private void selectFile(String title) {
        LayoutInflater inflaterDl = LayoutInflater.from(this);
        View layout = inflaterDl.inflate(R.layout.sb_state_dailog,
                null);
        // 对话框
        typeDialog = new Dialog(this);
        typeDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        typeDialog.show();

        typeDialog.setContentView(layout);
        ListView listView = (ListView) layout.findViewById(R.id.statesListView);
        TextView textView=(TextView)layout.findViewById(R.id.name);
        textView.setText(title);
        listView.setAdapter(new RicTypeAdapter(AddCywjActivity.this,ricList));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                typeDialog.dismiss();
                typeGw=ricList.get(position).getSNAME();
                gwType.setText(typeGw);

            }
        });

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Intent intent;
        switch (v.getId()) {
            case R.id.close:
                finish();
                break;

            case R.id.ritgh_text:
                String str1 = personID;
                String str2 = emilTitle.getText().toString();
                String str3 = emilContent.getText().toString();
                if (!Util.NoNull(str2)) {
                    Toast.makeText(getApplicationContext(), "传阅主题不能为空！", Toast.LENGTH_SHORT).show();
                } else if (!Util.NoNull(str1)) {
                    Toast.makeText(getApplicationContext(), "传阅人不能为空！", Toast.LENGTH_SHORT).show();
                } else if (!Util.NoNull(str3)) {
                    Toast.makeText(getApplicationContext(), "传阅内容不能为空！", Toast.LENGTH_SHORT).show();
                } else {

                    if (null==gwsf){
                        if (Util.NoNull(typeGw)&&Util.NoNull(fileId)){
                            dialog = ProgressDialog.show(this, null, "提交中...", true, false);
                            if (listFile.size()>0){
                                upFj(str2,str3,str1,fileId);
                            }else{
                                addCywj(str2,str3,str1,fileId);
                            }

                        }else{
                            Toast.makeText(getApplicationContext(), "公文类别或归档不能为空！", Toast.LENGTH_SHORT).show();
                        }
                    }else{
                        if (Util.NoNull(typeGw)&&Util.NoNull(fileId)){
                            dialog = ProgressDialog.show(this, null, "保存中...", true, false);
                            if (listFile.size()>0){
                                upFj(str2,str3,str1,fileId);
                            }else{
                                change(str2,str3,str1,fileId);
                            }

                        }else{
                            Toast.makeText(getApplicationContext(), "公文类别或归档不能为空！", Toast.LENGTH_SHORT).show();
                        }
                    }

                }
                break;

            case R.id.addFile:
                intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");//设置类型，我这里是任意类型，任意后缀的可以这样写。
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(intent, 1);
                break;
            case R.id.sendTo:
                startActivityForResult(new Intent(this,PersonActivity.class),2);
                break;

            case R.id.gwtype:
                if (fileTypeList!=null&&fileTypeList.size()>0){
                    selectFile("选择公文类别");

                }

                break;

            case R.id.gwFl:

                if (ricList!=null&&ricList.size()>0){
                    showtype("选择归档文件");
                }
                break;

            default:
                break;
        }
    }

    private Uri mPhotoUri = null;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {//是否选择，没选择就不会继续

            if (requestCode==1){
                Uri uri = data.getData();
                String  path= ImageUtlis.getRealPathFromUri(this,uri);
                if (null==path||path.length()<=0){
                    Toast.makeText(this,"获取文件失败",Toast.LENGTH_SHORT).show();
                    return;
                }
                File file = new File(path);
                listFile.add(file);
                String s = fuj.getText().toString();
                if (s.length() > 0) {
                    fuj.setText(s + "|" + file.getName());
                } else {
                    fuj.setText(file.getName());
                }
                Fjian fjian=new Fjian();
                fjian.setOLDNAME(file.getName());
                list.add(fjian);
                adapter.notifyDataSetChanged();
            }else if (requestCode==2){
                if (data != null) {
                    String str = data.getStringExtra("person");
                    personID = data.getStringExtra("personID");
                    System.out.println("personID---"+personID);
                    sendTo.setText(str);
                }
            }

//
        }
    }

    private String getPhotoPath(Context context, Uri photoUri, Intent data) {
        String photoPath = "";
        ContentResolver cr = context.getContentResolver();
        if (photoUri != null) {
            Cursor cursor = cr.query(photoUri, null, null, null, null);
            if (cursor != null && cursor.getCount() > 0) {
                cursor.moveToFirst();
                int pathIndex = cursor
                        .getColumnIndex(MediaStore.Images.Media.DATA);
                photoPath = cursor.getString(pathIndex);
                cursor.close();
            }
        } else {
            if (data != null) {
                Uri uri = data.getData();
                if (uri != null && !TextUtils.isEmpty(uri.getPath())) {
                    photoPath = uri.getPath();
                    if (!(new File(photoPath).exists())) {
                        String[] proj = {MediaStore.Images.Media.DATA};
                        Cursor actualimagecursor = cr.query(uri, proj, null,
                                null, null);
                        int actualImg = actualimagecursor
                                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                        actualimagecursor.moveToFirst();
                        if (actualimagecursor != null)
                            photoPath = actualimagecursor.getString(actualImg);
                    }
                }
            }
        }
        return photoPath;
    }


    /**
     * 提示
     */
    private void pointDialog(String str) {
        LayoutInflater inflaterDl = LayoutInflater.from(this);
        View layout = inflaterDl.inflate(R.layout.exti_dialog,
                null);
        // 对话框
        Jdialog = new Dialog(this);
        Jdialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        Jdialog.show();

        Jdialog.setContentView(layout);
        TextView sure = (TextView) layout.findViewById(R.id.sureExti);
        TextView cancle = (TextView) layout.findViewById(R.id.cancleExti);
        TextView dialogTitle = (TextView) layout.findViewById(R.id.dialogTitle);
        dialogTitle.setText(str);
        sure.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
//				ContextUtil.getInstance().finishApp();
                Jdialog.cancel();
                fuj.setText("");
                listFile.clear();


            }
        });
        cancle.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Jdialog.dismiss();
            }
        });

    }





    //提交
    public void addCywj(String title, String content, String tof,String fileId) {

        RequestParams params = new RequestParams(UConstants.ADD_CYWJ);
        params.addBodyParameter("LKind", "OA");
        params.addBodyParameter("EmpId", UConstants.EmpId);
        params.addBodyParameter("TITLE", title);
        params.addBodyParameter("CONTENT", content);
        params.addBodyParameter("JSEMPIDS", tof);
        params.addBodyParameter("DIRID", fileId);
        params.addBodyParameter("GWLX", typeGw);
        params.addBodyParameter("ID", idStr);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getDownRoute------------onSuccess-----", result);
                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);
                        boolean success = object.getBoolean("success");
//                        String msg = object.getString("msg");
                        if (success) {
                            dialog.dismiss();
                            Toast.makeText(x.app(), "提交成功！", Toast.LENGTH_LONG).show();
                            GwJsActivity.activity.setResh();
                           finish();
                        } else {
                            dialog.dismiss();
                            Toast.makeText(x.app(), "提交失败！", Toast.LENGTH_LONG).show();
                        }
//
                    } catch (JSONException e) {
                        dialog.dismiss();
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "提交出错！", Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
                dialog.dismiss();
            }

            @Override
            public void onFinished() {

            }
        });
    }



    //修改
    public void change(String title, String content, String tof,String fileId) {

        RequestParams params = new RequestParams(UConstants.GW_EDIT);
        params.addBodyParameter("LKind", "OA");
        params.addBodyParameter("EmpId", UConstants.EmpId);
        params.addBodyParameter("TITLE", title);
        params.addBodyParameter("CONTENT", content);
        params.addBodyParameter("JSEMPIDS", tof);
        params.addBodyParameter("DIRID", fileId);
        params.addBodyParameter("GWLX", typeGw);
        params.addBodyParameter("ID", idStr);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getDownRoute------------onSuccess-----", result);
                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);
                        boolean success = object.getBoolean("success");
//                        String msg = object.getString("msg");
                        if (success) {
                            dialog.dismiss();
                            Toast.makeText(x.app(), "修改成功！", Toast.LENGTH_LONG).show();
                            GwJsActivity.activity.setResh();
                            finish();
                        } else {
                            dialog.dismiss();
                            Toast.makeText(x.app(), "修改失败！", Toast.LENGTH_LONG).show();
                        }
//
                    } catch (JSONException e) {
                        dialog.dismiss();
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "提交出错！", Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
                dialog.dismiss();
            }

            @Override
            public void onFinished() {

            }
        });
    }

    //获取ID
    public void getId() {
        Util.printLog("------------UConstants.GET_ID-----", UConstants.GET_ID);
        RequestParams params = new RequestParams(UConstants.GET_ID);
//        params.addBodyParameter("LKind", "OA");
        params.addBodyParameter("TNAME", "ERPGWFS");


        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getDownRoute------------onSuccess-----", result);
                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);
                        boolean success = object.getBoolean("success");
                        String msg = object.getString("msg");
                        if (success) {
                            idStr=msg;

                        } else {
                            Toast.makeText(x.app(), "获取新增失败！", Toast.LENGTH_LONG).show();

                        }
//
                    } catch (JSONException e) {

                        Toast.makeText(x.app(), "获取新增失败！", Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "获取新增失败！", Toast.LENGTH_LONG).show();

            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");

            }

            @Override
            public void onFinished() {

            }
        });
    }

    //获取文件类型
    private void getFtype(final String type) {
        dialog = ProgressDialog.show(this, null, "加载中...", true, false);
        RequestParams params = new RequestParams(UConstants.GW_FILE_LIST);
        params.addBodyParameter("LKind", "OA");
        params.addBodyParameter("GWLX", type+"");
        params.addBodyParameter("Title", "");
        params.addBodyParameter("EmpId", UConstants.EmpId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getMerchants------------onSuccess-----", result);
                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);
                        boolean success = object.getBoolean("success");
//                        String msg = object.getString("msg");
                        if (success) {
                            Gson gson = new Gson();
                            Type lt = new TypeToken<List<FileType>>() {
                            }.getType();
                            fileTypeList  = gson.fromJson(object.getString("rows").toString(), lt);
//                            getJson(object.getString("rows").toString());
                            if (fileTypeList!=null&&fileTypeList.size()>0){
//                                fileTypeList=new ArrayList<FileType>();
//                                fileId=fileTypeList.get(0).getID();
//                                spinner2.setAdapter(new FtypeAdapter(AddCywjActivity.this, fileTypeList));
//                                spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//                                    @Override
//                                    public void onItemSelected(AdapterView<?> parent, View view,
//                                                               int position, long id) {
//                                        String str=parent.getItemAtPosition(position).toString();
//                                        System.out.print("spinner2----"+str);
//                                        fileId=fileTypeList.get(position).getID();
//
//                                    }
//                                    @Override
//                                    public void onNothingSelected(AdapterView<?> parent) {
//                                        // TODO Auto-generated method stub
//                                        System.out.print("BBBBBBBBBBBBBBBBBBBBB----");
//                                    }
//                                });
                            }
//                            FileType type1=new FileType();
//                            type1.setID("0");
//                            type1.setDICNAME("不归档查看");
//                            fileTypeList.add(type1);


                        } else {
                            Toast.makeText(x.app(), "暂无文件类型！", Toast.LENGTH_LONG).show();
                        }
//                        Toast.makeText(x.app(), msg, Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                dialog.dismiss();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "获取数据失败！", Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
                dialog.dismiss();
            }

            @Override
            public void onFinished() {
                dialog.dismiss();
            }
        });
    }



    //获取日程类型列表
    private void getRictype() {

        RequestParams params = new RequestParams(UConstants.GET_RIC_TYPE);
        params.addBodyParameter("LKind", "OA");
        params.addBodyParameter("SelId","GWLX");
//        params.addBodyParameter("PAGE",  "");
//        params.addBodyParameter("ROWS","");
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getMerchants------------onSuccess-----", result);
                if (result.length() > 0) {
                    try {
//                        JSONObject object = new JSONObject(result);
//                        boolean success = object.getBoolean("success");
////                        String msg = object.getString("msg");
//                        if (success) {
                        Gson gson = new Gson();
                        Type lt = new TypeToken<List<RicType>>() {
                        }.getType();
                        ricList = gson.fromJson(result.toString(), lt);
//                            getJson(object.getString("rows").toString());
                        if (null!=ricList&&ricList.size()>0){
//                            typeGw=list.get(0).getSNAME();
//                            spinner1.setAdapter(new RicTypeAdapter(AddCywjActivity.this,list));
//                            spinner1.setSelection(0, true);
//                            spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//                                @Override
//                                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                                    String str=parent.getItemAtPosition(position).toString();
//                                    System.out.print("spinner1----"+str);
//                                    typeGw=str;
//                                }
//
//                                @Override
//                                public void onNothingSelected(AdapterView<?> parent) {
//                                    System.out.print("AAAAAAAAAAAAAAA----");
//                                }
//                            });
//                            spinner1.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
//                                @Override
//                                public void onItemSelected(AdapterView<?> parent, View view,
//                                                           int position, long id) {
//                                    String str=parent.getItemAtPosition(position).toString();
//                                    System.out.print("spinner1----"+str);
//                                    typeGw=str;
//
//                                }
//                                @Override
//                                public void onNothingSelected(AdapterView<?> parent) {
//                                    // TODO Auto-generated method stub
//                                }
//                            });
                        }


//                        } else {
////                            Toast.makeText(x.app(), "暂无邮件！", Toast.LENGTH_LONG).show();
//                        }
//                        Toast.makeText(x.app(), msg, Toast.LENGTH_LONG).show();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

//                Toast.makeText(x.app(), "获取数据失败！", Toast.LENGTH_LONG).show();

            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");

            }

            @Override
            public void onFinished() {

            }
        });
    }


    //上传附件
    public void upFj(final String title, final String content, final String tof, String fileId) {

        RequestParams params = new RequestParams(UConstants.UP_FILI);
        params.addBodyParameter("LKind", "OA");
        params.addBodyParameter("TNAME", "ERPGWFS");
        params.addBodyParameter("XH",idStr );
        Util.printLog("UConstants.UP_FILI------------onSuccess-----", UConstants.UP_FILI);
        for (int i=0;i<listFile.size();i++){
            params.addBodyParameter("FILE",listFile.get(i));
        }
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getDownRoute------------onSuccess-----", result);
                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);
                        boolean success = object.getBoolean("success");

//                        String msg = object.getString("msg");
                        if (success) {
                            if (null==gwsf){
                                addCywj(title,content,tof,"0");
                            }else{
                                change(title,content,tof,"0");
                            }

                        } else {
                            Toast.makeText(x.app(), "附件上传失败！", Toast.LENGTH_LONG).show();
                        }
//
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(),  "附件上传出错！", Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
                dialog.dismiss();
            }

            @Override
            public void onFinished() {

            }
        });
    }




    //获取附件
    public void getFj() {
        Util.printLog("------------UConstants.GET_ID-----", UConstants.GET_ID);

        RequestParams params = new RequestParams(UConstants.GET_FJLIST);
        params.addBodyParameter("LKind", "OA");
        params.addBodyParameter("TNAME", "ERPGWFS");
        params.addBodyParameter("XH", gwsf.getID());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getDownRoute------------onSuccess-----", result);
                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);
                        boolean success = object.getBoolean("success");
//						String msg = object.getString("msg");
                        if (success) {
                            Gson gson = new Gson();
                            Type lt = new TypeToken<List<Fjian>>() {
                            }.getType();
                            list = gson.fromJson(object.getString("rows").toString(), lt);
                            if (list != null && list.size() > 0) {
                                num=list.size();
                                adapter=new FujAdapter(AddCywjActivity.this,list);
                                listFj.setAdapter(adapter);
                            }else{
                                list=new ArrayList<Fjian>();
                                adapter=new FujAdapter(AddCywjActivity.this,list);
                                listFj.setAdapter(adapter);
                            }


                        } else {
                            list=new ArrayList<Fjian>();
                            adapter=new FujAdapter(AddCywjActivity.this,list);
                            listFj.setAdapter(adapter);
                            Toast.makeText(x.app(), "获取附件失败！", Toast.LENGTH_LONG).show();

                        }
//
                    } catch (JSONException e) {
                        list=new ArrayList<Fjian>();
                        adapter=new FujAdapter(AddCywjActivity.this,list);
                        listFj.setAdapter(adapter);
                        Toast.makeText(x.app(), "获取附件失败！", Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");
                list=new ArrayList<Fjian>();
                adapter=new FujAdapter(AddCywjActivity.this,list);
                listFj.setAdapter(adapter);
                Toast.makeText(x.app(), "获取附件失败！", Toast.LENGTH_LONG).show();

            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");

            }

            @Override
            public void onFinished() {

            }
        });
    }



    //删除附件
    private void deleteFujian(final int positon) {
        dialog = ProgressDialog.show(this, null, "删除中...", true, false);
        RequestParams params = new RequestParams(UConstants.DELETE_FUJ);
        params.addBodyParameter("LKind", "OA");
        params.addBodyParameter("ID", list.get(positon).getID());
        params.addBodyParameter("TANME", list.get(positon).getTNAME());

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getMerchants------------onSuccess-----", result);
                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);

                        boolean success = object.getBoolean("success");
//
                        if (success) {
                            num--;
                            list.remove(positon);
                            adapter.notifyDataSetChanged();

                        } else {
                            Toast.makeText(x.app(), "删除失败！", Toast.LENGTH_LONG).show();
                        }
//
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                dialog.dismiss();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "删除失败！", Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
                dialog.dismiss();
            }

            @Override
            public void onFinished() {

            }
        });
    }

    //获取邮件收件人
    public void getjsPerson() {
        Util.printLog("------------UConstants.GET_ID-----", UConstants.GET_EMIL_PERSON);
        dialog = ProgressDialog.show(this, null, "加载中...", true, false);
        RequestParams params = new RequestParams(UConstants.GET_EMIL_PERSON);
//        params.addBodyParameter("LKind", "OA");
        params.addBodyParameter("ID", idStr);


        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getDownRoute------------onSuccess-----", result);
                if (result.length() > 0) {
//
                    Gson gson = new Gson();
                    Type lt = new TypeToken<List<EmilJs>>() {
                    }.getType();
                    jsList = gson.fromJson(result.toString(), lt);
                    if (null!=jsList&&jsList.size()>0){
                        for (int i=0;i<jsList.size();i++){
                            if (stringBufferId.length()<=0){
                                stringBufferId.append(jsList.get(i).getEMPID());
                                stringBufferN.append(jsList.get(i).getEMPNAME());
                            }else{
                                stringBufferId.append(",").append(jsList.get(i).getEMPID());
                                stringBufferN.append(",").append(jsList.get(i).getEMPNAME());
                            }
                        }
                        sendTo.setText(stringBufferN.toString());
                        personID=stringBufferId.toString();
                    }
                }
                dialog.dismiss();

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

//                Toast.makeText(x.app(), "获取新增失败！", Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

//                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
                dialog.dismiss();
            }

            @Override
            public void onFinished() {

            }
        });
    }
}
